# coding=utf8

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


class Preliminary:

    @staticmethod
    def demo_pyplot_proc():
        fig = plt.figure(num='demo figure', figsize=(8, 6))	    # 画板初始大小为（8，6），单位为英寸
        plt.plot(range(5), [np.random.randint(5, 10) for _ in range(5)])
        plt.xlabel("demo matplotlib pyplot proc")
        plt.show()
        # isclose = input("close figure?(y/n)")
        # if isclose.lower() in ['y', 'yes']:
        #     plt.close(fig)

    @staticmethod
    def demo_scatter():
        plt.figure('scatter')
        plt.scatter(df.age, df.height)
        plt.show()

    @staticmethod
    def demo_plot():
        plt.figure('plot')
        plt.plot(df.age, df.height)
        plt.show()

    @staticmethod
    def demo_bar():
        plt.figure('bar')
        plt.bar(df.age, df.height)
        plt.show()

    @staticmethod
    def demo_pie():
        plt.figure('pie')
        plt.pie([0.3, 0.4, 0.3])
        plt.show()


def task():
    df = pd.read_csv("stu84.csv", header=0, encoding='utf8')

    plt.figure("scatter")
    plt.scatter(x=df.age, y=df.height)
    plt.show()

    # 求各个年龄段的人数比例
    # --标识各个年龄段为Categorical类型，每个值为区间
    age_group = pd.cut(
        df.age,
        bins=[10, 13, 16, 18],
        labels=['11-13', '14-16', '17-18']
        )
    # --求各段人数、比例、排序
    age_group_count = age_group.value_counts()
    age_group_percentage = age_group_count / sum(age_group_count)
    age_group_percentage = age_group_percentage.sort_index()
    print(age_group_percentage)

    # 使用自定义函数标识
    # def data_seg(x):
    #     if 11 <= x <= 13:
    #         return 1
    #     elif 14 <= x <= 16:
    #         return 2
    #     else:
    #         return 3
    # df.loc[:, 'ageseg'] = df.age.apply(data_seg)
    # age_count = df.groupby('ageseg')['ageseg'].count()
    # age_count = age_count / age_count.sum()
    # print(age_count)

    # 使用autopct显示占比
    plt.figure("pie")
    plt.pie(age_group_percentage, autopct="%.2f")
    plt.show()


class training:

    @staticmethod
    def plot():
        df = pd.read_csv("stu84.csv", header=0, encoding='utf8')
        plt.figure("line")
        plt.plot(df.age, df.height)
        plt.show()

    @staticmethod
    def bar():
        df = pd.read_csv("stu84.csv", header=0, encoding='utf8')
        plt.figure("bar")
        plt.bar(df.age, height=df.height)
        plt.show()


if __name__ == "__main__":
    # Preliminary.demo_pyplot_proc()
    # Preliminary.demo_scatter()
    # Preliminary.demo_plot()
    # Preliminary.demo_bar()
    # Preliminary.demo_pie()
    # task()
    # training.plot()
    training.bar()
